package com.slipper.system.codeless.controller;

import com.slipper.core.tree.Tree;
import com.slipper.util.BeanUtil;
import com.slipper.util.Tool;
import jakarta.annotation.Resource;

import com.slipper.constants.Constants;
import com.slipper.system.codeless.bo.DataFolderBO;
import com.slipper.system.codeless.bo.DataModelBO;
import com.slipper.system.codeless.service.DataFolderService;
import com.slipper.system.codeless.service.DataModelService;
import com.slipper.core.tree.Node;
import com.slipper.core.tree.TreeInfo;
import com.slipper.core.tree.TreeUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.codeless.bo.DataFormBO;
import com.slipper.system.codeless.service.DataFormService;
import com.slipper.util.ResultUtil;

import java.util.List;

/**
 * 数据表单信息表
 */
@RestController
@RequestMapping("/system/dataForm")
public class DataFormController {
    @Resource
    private DataFormService dataFormService;
    @Resource
    private DataModelService dataModelService;
    @Resource
    private DataFolderService dataFolderService;
    
    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody DataFormBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(dataFormService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody DataFormBO bo) {
        return ResultUtil.successResult(dataFormService.queryList(bo));
    }
    
    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody DataFormBO bo) {
        return ResultUtil.successResult(dataFormService.queryPage(bo));
    }

    @PostMapping("/queryTree")
    public String queryTree() {
        List<DataFolderBO> folderList = dataFolderService.queryList(new DataFolderBO());
        TreeInfo folderTreeInfo = new TreeInfo("目录树");
        folderTreeInfo.setRootId(Constants.ROOT_ID);
        folderTreeInfo.setIdField("id");
        folderTreeInfo.setPidField("parentId");
        folderTreeInfo.setTextField("name");
        folderTreeInfo.setNodeType("folder");
        folderTreeInfo.setSortField("ordinal");

        DataModelBO dataModelBO = new DataModelBO();
        dataModelBO.setSortField("name asc");
        List<DataModelBO> modelList = this.dataModelService.queryList(dataModelBO);

        TreeInfo modelTreeInfo = new TreeInfo("");
        modelTreeInfo.setIdField("id");
        modelTreeInfo.setPidField("folderId");
        modelTreeInfo.setTextField("name");
        modelTreeInfo.setNodeType("model");
        modelTreeInfo.setSortField("ordinal");

        DataFormBO dataFormBO = new DataFormBO();
        dataFormBO.setSortField("name asc");
        List<DataFormBO> formList = this.dataFormService.queryList(dataFormBO);

        TreeInfo leafTreeInfo = new TreeInfo("");
        leafTreeInfo.setIdField("id");
        leafTreeInfo.setPidField("modelId");
        leafTreeInfo.setTextField("name");
        leafTreeInfo.setNodeType("form");
        leafTreeInfo.setSortField("ordinal");

        Node rootNode = TreeUtil.buildTree("asc",
                new Tree(folderTreeInfo, BeanUtil.setOrdinal(folderList)),
                new Tree(modelTreeInfo, BeanUtil.setOrdinal(modelList)),
                new Tree(leafTreeInfo, BeanUtil.setOrdinal(formList)));
        return ResultUtil.successResult(rootNode);
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody DataFormBO bo) {
        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(dataFormService.save(bo));
    }

    @PostMapping(value = "/copy")
    public String copy(@RequestBody DataFormBO bo) {
        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.operateResult(dataFormService.copy(bo), "复制");
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody DataFormBO bo) {
        return ResultUtil.deleteResult(dataFormService.delete(bo));
    }
}
